/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from "@mineadmin/form";
import MaUploadFile from "@/components/ma-upload-file/index.vue";
import MaRemoteSelect from "@/components/ma-remote-select/index.vue";
import MaDictSelect from "@/components/ma-dict-picker/ma-dict-select.vue";
import { concat } from "lodash-es";
import { page as getDevices } from "$/attractions/broadcast/api/broadcastDevices.ts";

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: any
): MaFormItem[] {
  
  // 根据重复类型决定执行日期字段的配置
  const getExecutionDayField = () => {
    const repeatType = model?.repeat_type;
    
    if (repeatType === 1) {
      // 不重复 - 日期选择器
      return {
        label: "执行日期",
        prop: "execution_day",
        render: "DatePicker",
        renderProps: {
          placeholder: "请选择执行日期",
          format: "YYYY-MM-DD",
          valueFormat: "YYYY-MM-DD",
        },
        mode: "date",
        showTime: false,
      };
    } else if (repeatType === 2) {
      // 每天 - 不显示字段或显示提示
      return {
        label: "执行日期",
        prop: "execution_day",
        render: "input",
        renderProps: {
          placeholder: "每天重复无需设置执行日期",
          disabled: true,
        },
      };
    } else if (repeatType === 3) {
      // 每周 - 输入框
      return {
        label: "执行日期",
        prop: "execution_day",
        render: "input",
        renderProps: {
          placeholder: "请输入周几，多个用逗号分隔，如：1,3,5（周一、周三、周五）",
        },
        itemProps: {
          help: "输入1-7表示周一到周日，多个值用逗号分隔"
        },
      };
    } else if (repeatType === 4) {
      // 每月 - 输入框
      return {
        label: "执行日期",
        prop: "execution_day",
        render: "input",
        renderProps: {
          placeholder: "请输入日期号，多个用逗号分隔，如：1,15,30（每月1号、15号、30号）",
        },
        itemProps: {
          help: "输入1-31表示月份中的日期，多个值用逗号分隔"
        },
      };
    } else {
      // 默认 - 通用输入框
      return {
        label: "执行日期",
        prop: "execution_day",
        render: "input",
        renderProps: {
          placeholder: "请先选择重复类型",
        },
      };
    }
  };

  return concat([
    {
      label: "计划描述",
      prop: "plan_description",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "计划描述" }) },
    },
    {
      label: "广播内容",
      prop: "content_text",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "广播内容" }),
      },
    },
    {
      label: "音频文件",
      prop: "content_audio_file",
      render: () => MaUploadFile,
      renderProps: {
        title: "选择音频文件",
        fileType: ["mp3", "wav", "flac", "aac", "ogg", "m4a", "wma"],
        fileSize: 50 * 1024 * 1024, // 50MB
        limit: 1,
        multiple: false,
      },
    },
    {
      label: "目标设备",
      prop: "target_devices",
      render: () => MaRemoteSelect,
      renderProps: {
        api: getDevices,
        apiParams: {
          page: 1,
          pageSize: 1000,
        },
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((device: any) => ({
              label: device.device_name || device.name || `设备${device.id}`,
              value: device.id,
            }));
          }
          return [];
        },
        placeholder: t("form.pleaseSelect", { msg: "目标设备" }),
        multiple: true,
        filterable: true,
        clearable: true,
      },
    },
    {
      label: "目标分组",
      prop: "target_groups",
      render: "input",
      renderProps: {
        placeholder: "请输入分组名称，多个分组用逗号分隔",
      },
      transform: {
        // 数组 -> 字符串（显示时）
        in: (value: any) => {
          if (Array.isArray(value)) {
            return value.join(",");
          }
          return value || "";
        },
        // 字符串 -> 数组（提交时）
        out: (value: string) => {
          if (typeof value === "string" && value.trim()) {
            return value
              .split(",")
              .map((item) => item.trim())
              .filter((item) => item);
          }
          return [];
        },
      },
    },
    {
      label: "开始时间",
      prop: "start_time",
      render: "TimePicker",
      renderProps: {
        placeholder: "请选择开始时间",
        format: "HH:mm:ss",
        valueFormat: "HH:mm:ss",
      },
    },
    {
      label: "结束时间",
      prop: "end_time",
      render: "TimePicker",
      renderProps: {
        placeholder: "请选择结束时间",
        format: "HH:mm:ss",
        valueFormat: "HH:mm:ss",
      },
    },
    {
      label: "重复类型",
      prop: "repeat_type",
      render: () => MaDictSelect,
      renderProps: {
        data: [
          { label: "不重复", value: 1 },
          { label: "每天", value: 2 },
          { label: "每周", value: 3 },
          { label: "每月", value: 4 },
        ],
        placeholder: "请选择重复类型",
        clearable: true,
        multiple: false,
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: "请选择重复类型",
          },
        ],
      },
    },
    getExecutionDayField(),

    {
      label: "重复间隔",
      prop: "repeat_interval",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "重复间隔（分钟）" }),
      },
    },
    {
      label: "重复次数",
      prop: "repeat_count",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "重复次数" }) },
      itemProps: { rules: [{ required: true, message: "请输入重复次数" }] },
    },
    {
      label: "触发事件",
      prop: "trigger_event",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "触发事件ID" }),
      },
    },
    {
      label: "触发条件",
      prop: "trigger_condition",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "触发条件（JSON格式）" }),
      },
    },
    {
      label: "音量",
      prop: "volume_day",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "音量" }) },
      itemProps: { rules: [{ required: true, message: "请输入音量" }] },
    },
  ]);
}
